﻿#include "httpresponse.h"
#include "const.h"
HttpResponse::HttpResponse()
    : m_redirectFlag(false), m_version(g_defaulthHttpVersion)
{}
HttpResponse::HttpResponse(int statu)
    : m_redirectFlag(false), m_statuCode(statu), m_version(g_defaulthHttpVersion)
{}
// 重置
void HttpResponse::reset()
{
    m_version = g_defaulthHttpVersion;
    m_headers.clear();
    m_body.clear();
}
// 设置头部字段
void HttpResponse::setHeader(const std::string &key, const std::string &value)
{   
    m_headers[key] = value;
}
// 设置正文
void HttpResponse::setBody(const std::string &body, const std::string &type)
{
    m_body = body;
    setHeader("Content-Type", type);
}
// 设置重定向
void HttpResponse::setRedirect(const std::string &url, int statu)
{
    m_statuCode = statu;
    m_redirectFlag = true;
    m_redirectUrl = url;
}
// 判断是否存在指定头部字段
bool HttpResponse::hasHeader(const std::string &key) const
{
    auto it = m_headers.find(key);
    if (it != m_headers.end())
    {
        return true;
    }
    return false;
}
// 获取指定头部字段
std::string HttpResponse::getHeader(const std::string &key) const
{
    auto it = m_headers.find(key);
    if (it != m_headers.end())
    {
        return it->second;
    }
    return "";
}
// 是不是短连接
bool HttpResponse::close() const
{
    // 没有Connection字段，或者有Connection但是值是close，则都是短链接，否则就是长连接
    if (hasHeader("Connection") == true && getHeader("Connection") == "keep-alive")
    {
        return false;
    }
    return true;
}